package pl.ivmx.cinema;

import java.lang.Thread.State;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class ThreadManager {

	
	private static ThreadManager instance ;
	private Map<DaoThread, Thread> mapa;
	
	private ThreadManager(){
		mapa = new HashMap<DaoThread, Thread>();
		
	};
	
	public static ThreadManager getInstance(){
		if (instance == null)
			instance = new ThreadManager();
		return instance;
	}
	
	/**
	 * 
	 * @return zwraca obiekt typu DaoThread
	 */
	public Runnable createThread(BaseDao parent){
		
		System.out.println("Tworze watek...");
		DaoThread daoT = new DaoThread(parent);
		Thread thread = new Thread( daoT );
		
		mapa.put(daoT, thread);
		
		
		return daoT;
		
		
	}
	public void start(Runnable thread){
		System.out.println("Startuje watek...");
		DaoThread daoT = (DaoThread) thread ;
		Thread th = mapa.get(daoT);
		
		if (th == null) {
			th = new Thread(daoT);
			mapa.put(daoT, th);
		}
		//DaoThread daoT = ;
		daoT.setRunning(true);
			th.start();
		
		System.out.println("Watek wystartowany...");
		
	}
	public void stopThread(Runnable thread){
		DaoThread daoT = (DaoThread) thread;
		daoT.setRunning(false);
		mapa.remove(daoT);
		
		
		
		System.out.println("Wylaczam watek...");
		
		
		
	
	}
}
